package com.crois.barrier.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crois.barrier.admin.entity.IpBlackWhiteList;
import com.crois.barrier.admin.enums.IpBlackWhiteTypeEnum;
import com.crois.barrier.admin.mapper.IpBlackWhiteListMapper;
import com.crois.barrier.common.exception.BarrierException;
import com.crois.barrier.common.exception.BarrierExceptionEnum;
import com.zhimydou.ztools.collection.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 17:42 2021/11/4
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class IpBlackWhiteSvc extends ServiceImpl<IpBlackWhiteListMapper,IpBlackWhiteList> implements IIpBlackWhiteSvc {


    @Autowired
    @Qualifier("nacosConfigSvc")
    private INaocsConfigSvc naocsConfigSvc;


    @Override
    public List<String> findIpBlackList() {
        List<String> ips = new ArrayList<>();
        QueryWrapper<IpBlackWhiteList> ipBlackWhiteListQueryWrapper = new QueryWrapper<>();
        ipBlackWhiteListQueryWrapper.lambda().eq(IpBlackWhiteList::getType,IpBlackWhiteTypeEnum.REFUSE.getType());
        List<IpBlackWhiteList> ipBlackLists = baseMapper.selectList(ipBlackWhiteListQueryWrapper);
        if (CollectionUtil.isNotEmpty(ipBlackLists)) {
            for (IpBlackWhiteList ipBlackList : ipBlackLists) {
                if (StringUtils.isNotEmpty(ipBlackList.getIps())) {
                    String blackIp[] = ipBlackList.getIps().split(",");
                    ips.addAll( Arrays.asList(blackIp));
                }
            }
        }
        return ips;
    }

    @Override
    public List<String> findIpWhiteList() {
        List<String> ips = new ArrayList<>();
        QueryWrapper<IpBlackWhiteList> ipBlackWhiteListQueryWrapper = new QueryWrapper<>();
        ipBlackWhiteListQueryWrapper.lambda().eq(IpBlackWhiteList::getType,IpBlackWhiteTypeEnum.ALLOW.getType());
        List<IpBlackWhiteList> ipWhiteLists = baseMapper.selectList(ipBlackWhiteListQueryWrapper);
        if (CollectionUtil.isNotEmpty(ipWhiteLists)) {
            for (IpBlackWhiteList ipWhiteList : ipWhiteLists) {
                if (StringUtils.isNotEmpty(ipWhiteList.getIps())) {
                    String ip[] = ipWhiteList.getIps().split(",");
                    ips.addAll( Arrays.asList(ip));
                }
            }
        }
        return ips;
    }


    @Override
    public List<IpBlackWhiteList> findIpBlackWhiteList() {
        return baseMapper.selectList(null);
    }


    @Override
    public void saveIpBlackWhite(IpBlackWhiteList ipBlackWhiteList) {
        int result = baseMapper.insert(ipBlackWhiteList);
        if (result==0) {
           throw new BarrierException(BarrierExceptionEnum.BARRIER_GATEWAY_IP_BLACK_WHITE_ADD_FAIL);
        }
        naocsConfigSvc.pushIpBlackWhite();
    }

    @Override
    public void updateIpBlackWhite(IpBlackWhiteList ipBlackWhiteList) {
        int result = baseMapper.updateById(ipBlackWhiteList);
        if (result==0) {
            throw new BarrierException(BarrierExceptionEnum.BARRIER_GATEWAY_IP_BLACK_WHITE_UPDATE_FAIL);
        }
        naocsConfigSvc.pushIpBlackWhite();
    }

    @Override
    public void removeIpBlackWhite(Integer ipBlackWhiteId) {
        int result = baseMapper.deleteById(ipBlackWhiteId);
        if (result==0) {
            throw new BarrierException(BarrierExceptionEnum.BARRIER_GATEWAY_IP_BLACK_WHITE_DELETE_FAIL);
        }
        naocsConfigSvc.pushIpBlackWhite();
    }
}
